import os
from pywinauto.application import Application
import pywinauto

def wxaddfri(telNum):
    # 通过CMD启动微信
    os.system(f'"C:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe"')
    # 连接微信
    app = Application(backend='uia').connect(class_name='WeChatMainWndForPC')
    # 获取窗口
    win_main_dialog = app.window(class_name='WeChatMainWndForPC')
    # win_main_dialog.print_control_identifiers()
    # searchMenu=win_main_dialog['Edit0'],优先使用child_window方法，减少延迟
    # 找到搜索框并点击
    searchMenu = win_main_dialog.child_window(title="搜索", control_type="Edit")
    searchMenu.click_input()
    # 全选并输入电话号码
    searchMenu.type_keys('^a').type_keys(telNum, with_spaces=True)
    search_Menu_Num = win_main_dialog.child_window(title="网络查找手机/QQ号", control_type="Text")
    search_Menu_Num.click_input()
    # 模拟鼠标点击添加到通讯录
    x = search_Menu_Num.rectangle().left + 310
    y = search_Menu_Num.rectangle().top + 115
    pywinauto.mouse.click(coords=(x, y))
    # 点击确认加好友
    pywinauto.mouse.click(coords=(x, y+420))
if __name__ == '__main__':
    wxaddfri('17673156819')